package cn.com.xxlibrary.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import cn.com.xxlibrary.dao.ISearchDao;
import cn.com.xxlibrary.util.UtilTools;

@Transactional
@Repository("searchDao")
public class SearchDaoImpl implements ISearchDao {

	@Autowired
	@Qualifier("sessionFactory")
	private SessionFactory sessionFactory;

	@Override
	public Map queryFromAll(int bt, int bd, String sValue, int start,
			int pageSize) {
		Session session = sessionFactory.getCurrentSession();
		String[] condition = sValue.split(" ");
		StringBuffer hql = new StringBuffer(
				"FROM BooksBean WHERE 1=1 ");


		for (String str : condition) {
			hql.append(" AND (bookBarCode LIKE ")
					.append(UtilTools.toSqlFuzzyString(str))
					.append(" OR bookCollection LIKE ")
					.append(UtilTools.toSqlFuzzyString(str))
					.append(" OR bookCallNo LIKE ")
					.append(UtilTools.toSqlFuzzyString(str))
					.append(" OR bookName LIKE ")
					.append(UtilTools.toSqlFuzzyString(str))
					.append(" OR bookChargePerson LIKE ")
					.append(UtilTools.toSqlFuzzyString(str))
					.append(" OR bookPublishing LIKE ")
					.append(UtilTools.toSqlFuzzyString(str))
					.append(" OR bookPublishingTime LIKE ")
					.append(UtilTools.toSqlFuzzyString(str))
					.append(" OR ISBN LIKE ")
					.append(UtilTools.toSqlFuzzyString(str))
					.append(" OR periNo LIKE ")
					.append(UtilTools.toSqlFuzzyString(str))
					.append(" OR perPeriods LIKE ")
					.append(UtilTools.toSqlFuzzyString(str)).append(")");
		}
		hql.append("ORDER BY bookStorageTime DESC");
		Query query = session.createQuery(hql.toString());
		int c = query.list().size();
		query.setFirstResult(start);
		query.setMaxResults(pageSize);
		List<?> lstResult = query.list();
		Map<Object, Object> mapResult = new HashMap<>();
		mapResult.put("queryResult", lstResult);
		mapResult.put("count", c);
		return mapResult;

	}

	@Override
	public Map queryFromOne(int bt, int bd, String sType, String sValue,
			int start, int pageSize) {
		Session session = sessionFactory.getCurrentSession();
		StringBuffer hql = new StringBuffer(
				"FROM BooksBean WHERE 1=1 ");
//		if (bt!=0){
//			hql.append(" AND bookType.bookTypeId=").append(bt);
//		}
//
//		if (bd!=0){
//			hql.append(" AND department.depId=").append(bd);
//		}

		hql.append(" AND ").append(sType)
				.append(" LIKE '%").append(sValue).append("%'");

		Query query = session.createQuery(hql.toString());
		int c = query.list().size();
		query.setFirstResult(start);
		query.setMaxResults(pageSize);
		List<?> lstResult = query.list();
		Map<Object, Object> mapResult = new HashMap<>();
		mapResult.put("queryResult", lstResult);
		mapResult.put("count", c);
		return mapResult;
	}


}
